package com.choosefine.it.mobile.salary.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.mobile.equipment.dto.ShoppingIdentityDTO;
import com.choosefine.it.mobile.salary.vo.WorkerSalaryAndSumVO;
import com.choosefine.it.mobile.salary.vo.WorkerSalaryDetailAndSumVO;
import com.choosefine.it.mobile.salary.vo.WorkerSalaryDetailVO;
import com.choosefine.it.mobile.salary.vo.WorkerSalaryVO;
import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.HttpServiceBean;
import com.choosefine.it.base.dto.DateDTO;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.common.utils.URLHandlerUtils;
import com.choosefine.it.team.model.vo.FinancialSumVO;
import com.choosefine.it.team.service.ItemInfoService;
import com.choosefine.it.worker.service.WorkerSalaryMonthService;
import com.github.pagehelper.Page;
import com.google.gson.Gson;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value="班组—我的分包工程-工人-班组全员|工人个人资报表",description="廖仉根")
@RestController
@RequestMapping("/team/salary")
public class WorkerSalaryController extends BaseController{
	
	@Autowired
	private HttpServiceBean httpServiceBean;
	@Autowired
	private ItemInfoService itemInfoService;
	@Autowired
	WorkerSalaryMonthService workerSalaryMonthService;
	
	@Value("#{yaml.get('month.team')}")
	private String monthTeam;
	@Value("#{yaml.get('month.countTeam')}")
	private String countTeam;
//	@Value("#{yaml.get('month.relation')}")
//	private String monthRelation;
	@Value("#{yaml.get('month.countRelation')}")
	private String countRelation;

	@ApiOperation(value="我的分包项目-工人-班组全员报表",response=WorkerSalaryAndSumVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="teamId",value="班组id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="projectCode",value="项目code",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="userCode",value="工人code过虑条件",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="isSum",value="是否需要班组数量默认(2)不需要:1需要，2不需要",defaultValue="2",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="date",value="时间年-月",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="起始页,默认1",defaultValue="1",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页行,默认10",defaultValue="10",required=false,dataType="Integer",paramType="query")
	})
	@GetMapping("/workerAllSalary")
	public String projectTeamAndSalary(
			@RequestParam(value="isSum",defaultValue="2")Integer isSum,
			@RequestParam(value="date",defaultValue="")String date,
			ShoppingIdentityDTO entityDto) throws Exception{
		if(null == entityDto.getTeamId() || entityDto.getTeamId().intValue() == 0)
			return responseFail(CommonConstant.TEAM_IS_NULL);
		
		PageDTO page = entityDto;
		StringBuffer teamUrl= URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  monthTeam);//定义url
		DateDTO dateDto=DateUtils.conversionDateDto(date, CommonConstant.DATE_FOMART);//app端时间转换
 		httpServiceBean.urlFormat(teamUrl, null, page, dateDto, entityDto);//url参数拼接
		String resultJS=httpServiceBean.getRequestBody(teamUrl.toString(), null);//发送请求
		String js=httpServiceBean.resultJsonFormat(resultJS, CommonConstant.DATA_KEY).toString();//接收参数格式化
		String array=httpServiceBean.resultJsonFormat(js, "array").toString();//接收参数格式化
	 
		if(StringUtils.isEmpty(js))
			return responseSuccess();
		
		Gson gs= new Gson();
		WorkerSalaryAndSumVO result = new WorkerSalaryAndSumVO();// 定义返回类
		List<Map> list = gs.fromJson(array, List.class);// 返回参数转化为list
		List<WorkerSalaryVO> resultList = new ArrayList<WorkerSalaryVO>();// 定义返回集合
		
		if (null != list) {
			for (Map nows : list) {// Map格式转为相应属性bean
				WorkerSalaryVO now = gs.fromJson(nows.toString(), WorkerSalaryVO.class);
				resultList.add(now);
			} 
		}
		
		Object objt = httpServiceBean.resultJsonFormat(js, CommonConstant.PAGE_COUNT);
		result.setPageCount(StringUtils.isEmpty(objt) ? 0 : Integer.parseInt(objt.toString()));
		result.setMembers(resultList);
		
		if(isSum==1){//需要总数
			StringBuffer capitalCount= URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  countTeam);//定义url
			httpServiceBean.urlFormat(capitalCount, null, page, dateDto, entityDto);//拼接参数
			String capitalCountJS=httpServiceBean.getRequestBody(capitalCount.toString(), null);//发送请求
			String count=httpServiceBean.resultJsonFormat(capitalCountJS, CommonConstant.DATA_KEY).toString();//格式化结果
			FinancialSumVO sum=gs.fromJson(count, FinancialSumVO.class);//结果放入返回类
			result.setSum(sum);
		}
		result.setWorkerCount(itemInfoService.getWorkerNumByTeamId(entityDto.getTeamId()));
		return responseSuccess(result);
	}
	
	@ApiOperation(value="我的分包项目-工人-工人个人月报表",response=WorkerSalaryDetailAndSumVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="date",value="时间年-月,默认截止到当月",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="isSum",value="是否需要班组数量默认)2(不需要1需要，2不需要",defaultValue="2",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="开始页,默认1",dataType="int",required=false,defaultValue="1",paramType = "query"),
		@ApiImplicitParam(name="pageSize",value="每页条数,默认10",dataType="int",required=false,defaultValue="10",paramType = "query")
	})
	@GetMapping("/relationSalary/{relationId}")
   public String selectUserByRelation(
    		@PathVariable(value="relationId",required=true)int relationId,
    		@RequestParam(value="isSum",defaultValue="2")Integer isSum,
    		@RequestParam(name="date",defaultValue="") String date,
    		PageDTO pageDTO) throws Exception{
		
		DateDTO dateDto=DateUtils.conversionDateDto(date, CommonConstant.DATE_FOMART);//app端时间转换
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("relationId",relationId);
    	map.put("startTime", dateDto.getStartTime());
    	map.put("endTime", dateDto.getEndTime());

		Gson gs= new Gson();
		WorkerSalaryDetailAndSumVO resultVO = new WorkerSalaryDetailAndSumVO();//定义返回类
		
		if (isSum == 1) {// 需要总数
			StringBuffer capitalCount= URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  countRelation);//定义url
			httpServiceBean.urlFormat(capitalCount, map, null, dateDto, null);//拼接参数
			String capitalCountJS=httpServiceBean.getRequestBody(capitalCount.toString(), null);//发送请求
			String count=httpServiceBean.resultJsonFormat(capitalCountJS, CommonConstant.DATA_KEY).toString();//格式化结果
			FinancialSumVO sum=gs.fromJson(count, FinancialSumVO.class);//结果放入返回类
			resultVO.setSum(sum);
		}
		
		List<WorkerSalaryDetailVO> resultList = null;
		//循环出每月的小计
 		Page<Map> everyMonthly = workerSalaryMonthService.queryEveryMonthsByRelationId(map, pageDTO);
//		StringBuffer relationUrl = URLHandlerUtils.getUrlBuffer(shixiaobaoURL,  monthRelation);//定义url
// 		for (Map param : everyMonthly) {
// 			map.clear();
// 			//根据每月的小计得出每月的天明细
// 			map.put("workerSalaryMonthId", param.get("id"));
// 	 		httpServiceBean.urlFormat(relationUrl, map, null, null, null);//url参数拼接
// 	 		
// 	 		String resultJS=httpServiceBean.getRequestBody(relationUrl.toString(), null);//发送请求
// 			String js=httpServiceBean.resultJsonFormat(resultJS, CommonConstant.DATA_KEY).toString();//接收参数格式化
// 			List<Map> list = gs.fromJson(js, List.class);//返回参数转化为list
//			resultList = new ArrayList<WorkerSalaryDetailVO>();// 定义返回集合
//			for (Map nows : null == list ? new ArrayList<Map>() : list) {// Map格式转为相应属性bean
// 				WorkerSalaryDetailVO now= gs.fromJson(nows.toString(),WorkerSalaryDetailVO.class);
// 				resultList.add(now);
// 			}
// 			
// 			resultVO.getDaySalaryList().add(resultList);
// 			resultJS = null;
// 			js = null;
// 			resultList = null;
// 			relationUrl.delete(0, relationUrl.length());
// 			relationUrl.append(this.monthRelation);
//		}
 		resultVO.setMonthSalaryList(everyMonthly);
 		resultVO.setMonthSalaryCount(everyMonthly.getPages());
		return responseSuccess(resultVO);
	}
	
}
